package com.dati.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import util.AllTypeList;
import util.Constant;
import util.TotalClass;
import util.UtilDAO;

import com.UserModule.bean.TbUserBean;
import com.dati.bean.Answer;
import com.dati.bean.DatiMiddle;
import com.dati.bean.DatiType;
import com.dati.bean.Xulie;
import com.dati.dao.DatiDao;
import com.dati.dao.DatiTypeDao;
import com.dianbo.bean.DianboType;
import com.jifen.bean.JiFen;
import com.jifen.dao.JifenDao;

public class DatiTypeServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public DatiTypeServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		//response.setContentType("text/html");
		response.setContentType("text/html;charset=UTF-8");
		TbUserBean user = (TbUserBean)request.getSession().getAttribute("User");
		PrintWriter out = response.getWriter();
		String method=request.getParameter("method");
		if(method.equals("addType")){//新增一答题类型
			response.sendRedirect("jifen/addDatiType.jsp");
		}else if(method.equals("addTypeSubmit")){//新增答题类型提交
			String datiTypeName = Constant.decodeString(request.getParameter("datiTypeName"));
			DatiType datiType = new DatiType();
			String idNum = Constant.getNum();
			datiType.setAnswerTypeId(idNum);
			datiType.setCreateTime(Constant.decodeDate());
			datiType.setTypeName(datiTypeName);
			datiType.setUserId(user.getUserId());
			boolean bo = new DatiTypeDao().addDatiType(datiType);
			
			List list = new AllTypeList().getLastValue(user.getUserId());
			List<DianboType> dianboTypeList = (ArrayList<DianboType>)list.get(0);
			StringBuffer sb = (StringBuffer)list.get(1);
			
		    request.getSession().setAttribute("result", "答题类型新建成功!");
		    request.getSession().setAttribute("dianboTypeList", dianboTypeList);
		    request.getSession().setAttribute("sb", sb);
		    response.sendRedirect("jifen/jiFenupLoadList.jsp");
		}else if(method.equals("modifyType")){//在修改类型时的显示操作
			String typeId = request.getParameter("typeId");
			DatiType datiType = new DatiTypeDao().getDatiType(typeId);
			request.getSession().setAttribute("datiType", datiType);
			response.sendRedirect("jifen/modifyDatiType.jsp?num="+new Random().nextInt(10000));
		}else if(method.equals("modifyTypeSubmit")){//修改类型的提交功能
			String datiTypeName = Constant.decodeString(request.getParameter("datiTypeName"));
			String typeId = request.getParameter("datiType");
			DatiType datiType = new DatiType();
			datiType.setCreateTime(Constant.decodeDate());
			datiType.setAnswerTypeId(typeId);
			datiType.setTypeName(datiTypeName);
			datiType.setUserId(user.getUserId());
			boolean bo = new DatiTypeDao().modifyDatiType(datiType);
			
			List list = new AllTypeList().getLastValue(user.getUserId());
			List<DianboType> dianboTypeList = (ArrayList<DianboType>)list.get(0);
			StringBuffer sb = (StringBuffer)list.get(1);
			if(bo){
				 request.getSession().setAttribute("result", "答题类型修改成功!");
			}else{
				 request.getSession().setAttribute("result", "答题类型修改失败!");
			}
		   
		    request.getSession().setAttribute("dianboTypeList", dianboTypeList);
		    request.getSession().setAttribute("sb", sb);
		    response.sendRedirect("jifen/jiFenupLoadList.jsp");
		}else if(method.equals("deleteType")){//删除答题类型
			String datiTypeId = request.getParameter("typeId");
			System.out.println("删除=datiType==="+datiTypeId);
			String xilieCondition=" answerTypeId='"+datiTypeId+"'";
			List<Xulie> xilieList = new DatiTypeDao().getXulieByTypeId("tb_serise", "", xilieCondition);
			String seriseIds = "";
			if(xilieList!=null&&xilieList.size()>0){
				for(Xulie xilie:xilieList){
					if(seriseIds.equals("")){
						seriseIds+=xilie.getSeriseId();
					}else{
						seriseIds+=","+xilie.getSeriseId();
					}
				}
				String datiMidCondition=" seriseId in("+seriseIds+")";
				List<DatiMiddle> datiMidList = new DatiDao().getDatiMiddle("tb_answer_middle", "", datiMidCondition);
				String middleIds = "";
				if(datiMidList!=null&&datiMidList.size()>0){
					for(DatiMiddle datiMid:datiMidList){
						if(middleIds.equals("")){
							middleIds+=datiMid.getAnswerMiddleId();
						}else{
							middleIds+=","+datiMid.getAnswerMiddleId();
						}
					}
					String answerCondition= " tb_answer where answerMiddleId in("+middleIds+")";
					boolean answerBo = UtilDAO.delAll(answerCondition);
					if(answerBo){
						String midCondition=" tb_answer_middle where answerMiddleId in("+middleIds+")";
						boolean middBo = UtilDAO.delAll(midCondition);
						if(middBo){
							String xiLCondition=" tb_serise where seriseId in("+seriseIds+")";
							boolean xilBo = UtilDAO.delAll(xiLCondition);
							if(xilBo){
								boolean boo = UtilDAO.del("tb_answer_type", "answerTypeId", datiTypeId);
								if(boo){
									request.getSession().setAttribute("result", "答题类型删除成功");
								}else{
									request.getSession().setAttribute("result", "答题类型删除失败");
								}
							}else{
								request.getSession().setAttribute("result", "由于删除答题系列错误导致答题类型删除失败");
							}
						}else{
							request.getSession().setAttribute("result", "由于删除答题内容错误导致答题类型删除失败");
						}
					}else{
						request.getSession().setAttribute("result", "由于删除答题错误导致答题类型删除失败");
					}
				}else{
					String xiLCondition=" tb_serise where seriseId in("+seriseIds+")";
					boolean xilBo = UtilDAO.delAll(xiLCondition);
					if(xilBo){
						boolean boo = UtilDAO.del("tb_answer_type", "answerTypeId", datiTypeId);
						if(boo){
							request.getSession().setAttribute("result", "答题类型删除成功");
						}else{
							request.getSession().setAttribute("result", "答题类型删除失败");
						}
					}else{
						request.getSession().setAttribute("result", "由于答题系列删除错误导致答题类型删除失败");
					}
				}
				
			}else{
				boolean boo = UtilDAO.del("tb_answer_type", "answerTypeId", datiTypeId);
				if(boo){
					request.getSession().setAttribute("result", "答题类型删除成功");
				}else{
					request.getSession().setAttribute("result", "答题类型删除失败");
				}
			}
			
			
			/*boolean bo = UtilDAO.del("tb_serise", "answerTypeId", datiTypeId);
			if(bo){
				boolean boo = UtilDAO.del("tb_answer_type", "answerTypeId", datiTypeId);
				if(boo){
					request.getSession().setAttribute("result", "删除成功!");
				}else{
					request.getSession().setAttribute("result", "删除失败!");
				}
			}else{
				request.getSession().setAttribute("result", "删除失败!");
			}*/
			List list = new AllTypeList().getLastValue(user.getUserId());
			List<DianboType> dianboTypeList = (ArrayList<DianboType>)list.get(0);
			StringBuffer sb = (StringBuffer)list.get(1);
			request.getSession().setAttribute("dianboTypeList", dianboTypeList);
			request.getSession().setAttribute("sb", sb);
			response.sendRedirect("jifen/jiFenupLoadList.jsp");
		}else if(method.equals("addXiLie")){
			String typeId = request.getParameter("typeId");
			request.getSession().setAttribute("typeId", typeId);
			response.sendRedirect("jifen/addXiLie.jsp?num="+new Random().nextInt(10000000));
		}else if(method.equals("addXilieSubmit")){//新增系列提交
			String typeId=request.getParameter("typeId");
			String xilieName = Constant.decodeString(request.getParameter("xilieName"));
			Xulie xulie = new Xulie();
			xulie.setAnswerTypeId(typeId);
			xulie.setCreateTime(Constant.decodeDate());
			xulie.setSeriseId(Constant.getNum());
			xulie.setSeriseName(xilieName);
			boolean bo = new DatiTypeDao().addXulie(xulie);
			
			List list = new AllTypeList().getLastValue(user.getUserId());
			List<DianboType> dianboTypeList = (ArrayList<DianboType>)list.get(0);
			StringBuffer sb = (StringBuffer)list.get(1);
			if(bo){
				 request.getSession().setAttribute("result", "答题系列新建成功!");
			}else{
				 request.getSession().setAttribute("result", "答题系列新建失败!");
			}
		   
		    request.getSession().setAttribute("dianboTypeList", dianboTypeList);
		    request.getSession().setAttribute("sb", sb);
		    response.sendRedirect("jifen/jiFenupLoadList.jsp?num="+new Random().nextInt(10000000));
		}else if(method.equals("modifyXilie")){//修改系列
			String xilieId = request.getParameter("xilieId");
			System.out.println("xilieId==="+xilieId);
			Xulie xl = new DatiTypeDao().getXulie(xilieId);
			request.getSession().setAttribute("xilie", xl);
			response.sendRedirect("jifen/modifyDatiXilie.jsp?num="+new Random().nextInt(10000));
		}else if(method.equals("modifyXilieSubmit")){//修改系列提交
			String xilieId = request.getParameter("xilieId");
			String xilieName = Constant.decodeString(request.getParameter("xilieName"));
			Xulie xl = new Xulie();
			xl.setCreateTime(Constant.decodeDate());
			xl.setSeriseId(xilieId);
			xl.setSeriseName(xilieName);
			boolean bo = new DatiTypeDao().modifyXulie(xl);

			List list = new AllTypeList().getLastValue(user.getUserId());
			List<DianboType> dianboTypeList = (ArrayList<DianboType>)list.get(0);
			StringBuffer sb = (StringBuffer)list.get(1);
			if(bo){
				 request.getSession().setAttribute("result", "答题系列修改成功!");
			}else{
				 request.getSession().setAttribute("result", "答题系列修改失败!");
			}
		   
		    request.getSession().setAttribute("dianboTypeList", dianboTypeList);
		    request.getSession().setAttribute("sb", sb);
		    response.sendRedirect("jifen/jiFenupLoadList.jsp?num="+new Random().nextInt(10000000));
		}else if(method.equals("deleteXilie")){//删除某个系列
			String xilieId = request.getParameter("xilieId");
			//boolean bo = UtilDAO.del("tb_serise", "seriseId", xilieId);

			String datiMidCondition=" seriseId ='"+xilieId+"'";
			List<DatiMiddle> datiMidList = new DatiDao().getDatiMiddle("tb_answer_middle", "", datiMidCondition);
			String middleIds = "";
			if(datiMidList!=null&&datiMidList.size()>0){
				for(DatiMiddle datiMid:datiMidList){
					if(middleIds.equals("")){
						middleIds+=datiMid.getAnswerMiddleId();
					}else{
						middleIds+=","+datiMid.getAnswerMiddleId();
					}
				}
				String answerCondition= " tb_answer where answerMiddleId in("+middleIds+")";
				boolean answerBo = UtilDAO.delAll(answerCondition);
				if(answerBo){
					String midCondition=" tb_answer_middle where answerMiddleId in("+middleIds+")";
					boolean middBo = UtilDAO.delAll(midCondition);
					if(middBo){
						String xiLCondition=" tb_serise where seriseId ='"+xilieId+"'";
						boolean xilBo = UtilDAO.delAll(xiLCondition);
							if(xilBo){
								request.getSession().setAttribute("result", "系列删除成功");
							}else{
								request.getSession().setAttribute("result", "系列删除失败");
							}
						}else{
							request.getSession().setAttribute("result", "由于删除答题内容错误导致答题系列删除失败");
						}
					}else{
						request.getSession().setAttribute("result", "由于删除答题错误导致答题系列删除失败");
					}
				}else{
					String xiLCondition=" tb_serise where seriseId ='"+xilieId+"'";
					boolean xilBo = UtilDAO.delAll(xiLCondition);
						if(xilBo){
							request.getSession().setAttribute("result", "系列删除成功");
						}else{
							request.getSession().setAttribute("result", "系列删除失败");
						}
				}
				
			
			
			List list = new AllTypeList().getLastValue(user.getUserId());
			List<DianboType> dianboTypeList = (ArrayList<DianboType>)list.get(0);
			StringBuffer sb = (StringBuffer)list.get(1);
			
		   
		    request.getSession().setAttribute("dianboTypeList", dianboTypeList);
		    request.getSession().setAttribute("sb", sb);
		    response.sendRedirect("jifen/jiFenupLoadList.jsp?num="+new Random().nextInt(10000000));
		}else if(method.equals("setJiFen")){//设置积分
			String xilieId = request.getParameter("xilieId");
		    Xulie  xi = new DatiTypeDao().getXulie(xilieId);
		    request.getSession().setAttribute("xilie", xi);
			response.sendRedirect("jifen/jiFenupLoadSetDati.jsp?num="+new Random().nextInt(10000000));
		}else if("getXilieByTypeId".equals(method)){//根据答题类型的ID得到此类型的系列
			//response.setContentType("Content-Type:text/html;charset=utf-8");  
			String typeId = request.getParameter("typeId");
			System.out.println("typeId=="+typeId);
			String condition="answerTypeId='"+typeId+"'";//
			List<Xulie> xilieList = new DatiTypeDao().getXulieByTypeId("tb_serise", "createTime", condition);
			StringBuffer sb = new StringBuffer();
			sb.append("<select id=\"xilieSelect\" name=\"xilieSelect\" onchange=\"selectXilie(xilieSelect.value)\">");
			sb.append("<option value=\"0\">选择系列</option>");
			if(xilieList!=null&&xilieList.size()>0){
				for(Xulie xilie:xilieList){
					sb.append("<option value=\""+xilie.getSeriseId()+"\">"+xilie.getSeriseName()+"</option>");
				}
			}
			sb.append("</select><input type=\"hidden\" name=\"xilie_Type\" id=\"xilie_Type\" value=\"0\"/>");
			System.out.println("sb=="+sb);
			String lastStr = URLEncoder.encode(sb.toString(), "UTF-8");
			String str = lastStr.replaceAll("\\+", " ");
			System.out.println("lastStr=="+str);
			out.println(str);
			//out.println("张庆玉");
		}else if("getXilieValue".equals(method)){
			String xilieId = request.getParameter("xilieId");
			String condition = "seriseId='"+xilieId+"' and integralType=0";
			JiFen jifen = new JifenDao().getJiFen("tb_integral", "setTime", condition);
			String fenshu = "";
			if(jifen==null){
				fenshu="0";
			}else
			fenshu = jifen.getIntegralValue();
			out.println(fenshu);
		}
	}
	
	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		doGet(request,response);
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

	/**
	 *@author qingyu zhang
	 *@function:
	 *2011-4-4
	 *下午02:41:24
	 *zzClub
	 *com.dati.servlet
	 */
}
